﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Map
{
    class Map
    {
        private Texture2D texture;
        //the quad of X and Y 
        public static List<List<Quadrent>> Quad = new List<List<Quadrent>>();
        public void mapDivision(Texture2D Texture)
        {

            
            for (int X= 0;  X <(texture.Width/100); X++)
            {
                Quad.Add(new List<Quadrent>());
                for (int Y = 0; Y < (texture.Height / 100); Y++)
                {
                    Quad[X].Add(new Quadrent(0,new Vector2(X,Y)));

                    Color[] color = new Color[100*100];

                    texture.GetData<Color>(0, new Rectangle((X * 100), (Y * 100), 100,100), color, 0,color.Length);

                    Quad[X][Y].Texture.SetData<Color>(color);
           
                }
                
            }
        }



        public void Load(ContentManager Content)
        {
            texture = Content.Load<Texture2D>("Map//map");
            mapDivision(texture);
        }

        public Color GetPixel(Vector2 Location)
        {
            Color[] Pixel = new Color[1];

            Vector2 TileLocation = Location / new Vector2(100, 100);
            Vector2 PixelLocation = new Vector2(Location.X % 100, Location.Y % 100);
            Quad[(int)TileLocation.X][(int)TileLocation.Y].Texture.GetData<Color>(1,new Rectangle(((int)PixelLocation.X),((int)PixelLocation.Y),1,1),Pixel,1,1);

            return Pixel[0];
        }
        public void DeletePixel(Vector2 Location)
        {
            Color[] pixel = new Color[1];
            pixel[0] = new Color(0,0,0,0);

            Vector2 TileLocation = Location / new Vector2(100, 100);
            Vector2 PixelLocation = new Vector2(Location.X % 100, Location.Y % 100);
            Quad[(int)TileLocation.X][(int)TileLocation.Y].Texture.SetData<Color>(1, new Rectangle(((int)PixelLocation.X), ((int)PixelLocation.Y), 1, 1), pixel, 1, 1);

        }
      
        public void Draw(SpriteBatch sprites)
        {

            for (int X = (int)((Cam.Camera.Location.X - (Globals.Globals.Screen_Width / 2)) / 100)  ; X < (Globals.Globals.Screen_Width / 100); X++)
            {
                for (int Y = (int)((Cam.Camera.Location.Y - Globals.Globals.Screen_Height / 2) / 100); Y < (Globals.Globals.Screen_Height / 100); Y++)
                {
                    if (X >= 0 && X  <Quad.Count  )
                    {
                        if (Y  >= 0 && Y < Quad[0].Count )
                        {
                            sprites.Draw(Quad[X][Y].Texture, Quad[X][Y].Location * new Vector2(100, 100), Color.White);
                        }
                    }
                }
            }
              
            
         }
    
     
    }
}
